<template>
	<view class="event-registration-page">
		<view class="title">{{formData.title}}</view>
		<view>
			<view class="info-area">
				<view>报名时间: <text>{{formData.retB}}至{{formData.retA}}</text>
				</view>
				<view>参赛人数: <text>5 - {{formData.participantsNum}}</text></view>
				<view>比赛时间:
					<text>{{formData.ratB}}至{{formData.ratA}}</text>
				</view>
				<view>赛事地点：<text>{{formData.raceAddress}}</text></view>
			</view>
			<view style="margin-top: 12px;">
				<view>
					报名球队：
<!-- 					<uni-data-select collection="team-MGMT" field="_id as value, teamName as text"
						orderby="text asc" :where="ballTmeamWhere" :defItem="1" v-model="formData.ballTeamNm"
						:clear="false" /> -->
						<uni-data-select :defItem="1" v-model="formData.ballTeamNm" :localdata="teamList"
							:clear="false" />
				</view>
				<view>
					<view style="margin: 22px 0;">参赛球员：</view>
					<uni-data-checkbox multiple v-model="formData.playersList" :localdata="playersList" />
				</view>
			</view>
		</view>
		<view class="bottom-area-sty" v-if="userInfo.userType == 2 && formData.state == 0">
			<view class="bm-btn-sty" @click="entryConfirmBtn">立即报名</view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCmd = db.command;
	export default {
		data() {
			return {
				formData: {},
				img: '',
				userInfo: {},
				playersList: [],
				teamList: [],
				ballTmeamWhere: '',
				userWhere: ''
			}
		},
		onLoad(op) {
			const _this = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					console.log(res.data)
					if (res.data) {
						_this.userInfo = res.data
						_this.ballTmeamWhere =
							`"deleted_at"==null&&"state"==1&&"_id"=="${_this.userInfo.ballTeamNm}"`
						_this.userWhere = `"deleted_at"==null&&"ballTeamNm"=="${_this.userInfo.ballTeamNm}"`
						_this.getPlayersList()
						_this.getBallTeamInfo()
					}
				}
			})
			this.formData = JSON.parse(op.parm)
			this.formData.retB = this.formData.regTime[0].substring(0, 10)
			this.formData.retA = this.formData.regTime[1].substring(0, 10)
			this.formData.ratB = this.formData.raceTime[0].substring(0, 10)
			this.formData.ratA = this.formData.raceTime[1].substring(0, 10)
			// this.formData.ballTeamNm = []
			this.formData.playersList = this.formData.listEntries ? this.formData.listEntries.split(','):[]
		},
		created(op) {},
		mounted(op) {},
		methods: {
			getBallTeamInfo () {
				db.collection('team-MGMT').where(this.ballTmeamWhere).field(
					"teamName"
				).get().then((res) => {
					const data = []
					res.result.data.map(item => {
						data.push({
							text: item.teamName,
							value: item._id
						})
					})
					this.teamList = data
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {})
			},
			getPlayersList() {
				db.collection('user-MGMT').where(this.userWhere).field(
					"userNm,deleted_at"
				).get().then((res) => {
					const data = res.result.data
					if (data) {
						const arr = []
						res.result.data.map(item => {
							arr.push({
								text: item.userNm,
								value: item._id
							})
						})
						this.playersList = arr
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {})
			},
			// 记录参赛球队数
			addEntryNum() {
				const n = this.formData.entryNum ? this.formData.entryNum*1 : 0
				const parm = {
					entryNum: `${n + 1}`
				}
				return db.collection('competition-MGMT').doc(this.formData.id).update(parm).then((res) => {
					setTimeout(() => uni.navigateBack(), 2283)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			entryConfirmBtn() {
				console.log(this.formData)
				const where = `"raceId" == "${this.formData.id}"&&"ballTeamId"=="${this.formData.ballTeamNm}"`
				if (!this.formData.ballTeamNm) {
					uni.showModal({
						content: '请选择球队',
						showCancel: false
					})
					return
				}
				if (!this.formData.playersList) {
					uni.showModal({
						content: '请选添加参赛球员！',
						showCancel: false
					})
					return
				}
				
				// if选的球员<最低人数 || 选的球员>限定人数则提示
				if ((this.formData.playersList.length < 1) || (this.formData.playersList.length > this
						.formData
						.participantsNum * 1)) {
					uni.showModal({
						content: '请选择正确数量的球员',
						showCancel: false
					})
					return
				}
				let parm = {
					title: this.formData.title,
					raceId: this.formData.id,
					raceType: this.formData.typ,
					raceStatus: this.formData.stateNm,
					ballTeamId: this.formData.ballTeamNm,
					ballTeamNm: this.teamList.find(item => item.value == this.formData.ballTeamNm).text,
					listEntries: this.formData.playersList.join(','),
					deleted_at: null,
					created_date: new Date().getTime(),
					updated_date: null,
					created_by: this.userInfo.userNm
				}
				db.collection('event-registration').where(where).field("raceId,ballTeamId,changePlayer").get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						const text = data.changePlayer == 0 || !data.changePlayer ? '已报名！不可重复报名！': '已报名,是否修改报名信息？'
						uni.showModal({
							content: text,  // '已报名,是否修改报名信息？',
							success: function (res) {
								if (res.confirm) {
									if (data.changePlayer == 1) {
										db.collection('event-registration').doc(data._id).update(parm).then((res) => {
											uni.showToast({
												title: '修改成功'
											})
											setTimeout(() => uni.navigateBack(), 2283)
										}).catch((err) => {
											uni.showModal({
												content: err.message || '请求服务失败',
												showCancel: false
											})
										})
									}
								} else {
									return
								}
							}
						})
						return
					} else {
						// raceType: raceStatus: ballTeamNm: deleted_at: updated_date:ballTeamType: 
						return db.collection('event-registration').add(parm).then((res) => {
							uni.showToast({
								title: '报名成功'
							})
							this.addEntryNum()
						}).catch((err) => {
							uni.showModal({
								content: err.message || '请求服务失败',
								showCancel: false
							})
						})
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.event-registration-page {
		height: 100vh;
		padding: 30rpx;
	}

	.title {
		margin: 7px 0 22px;
		font-size: 36rpx;
		font-weight: 600;
	}

	.info-area {
		font-size: 26rpx;
		color: #aaa;

	}

	.info-area view {
		margin-bottom: 12px;
	}

	.info-area text {
		display: inline-block;
		margin-left: 30rpx;
		color: #333;
	}

	.event-registration-page .uni-stat__select {
		width: 65%;
		display: inline-block;
	}

	.event-registration-page .uni-data-checklist .checklist-group .checklist-box {
		width: 40%;
	}

	.event-registration-page .uni-data-checklist .checklist-group .checklist-box .checklist-content .checklist-text {
		font-size: 30rpx;
	}

	.bottom-area-sty {
		width: 100%;
		height: 45px;
		background-color: #fff;
		position: fixed;
		bottom: 0;
	}

	.bm-btn-sty {
		width: 50%;
		height: 45px;
		text-align: center;
		background-color: #ffa52d;
		color: #fff;
		line-height: 45px;
		font-size: 32rpx;
		float: right;
	}
</style>
<style lang="scss">
</style>